package edu.bbu.bibliospring.api.resources;

import edu.bbu.bibliospring.api.cdiassembler.Assembler;
import edu.bbu.bibliospring.api.cdiassembler.AuthorAssembler;
import edu.bbu.bibliospring.api.exception.BibliospringApiException;
import edu.bbu.bibliospring.backend.model.Author;
import edu.bbu.bibliospring.backend.service.AuthorService;
import edu.bbu.bibliospring.backend.service.exception.BibliospringServiceException;
import edu.bbu.bibliospring.common.dto.AuthorDto;
import org.apache.log4j.Logger;

import javax.ejb.DependsOn;
import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;
import javax.ws.rs.*;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by anna on 5/26/14.
 */

@LocalBean
@Stateless
@DependsOn({"AuthorService","AuthorAssembler"})
@Path("/authors")
@Produces(MediaType.APPLICATION_JSON)
public class AuthorResource {

    private static final Logger LOG = Logger.getLogger(TestResource.class);
	
	@EJB
	private AuthorService authorService;

    private final Assembler<Author, AuthorDto> authorAssembler = new AuthorAssembler();


	@Path("/{authorId}")
	@GET
	//JAX RS annotacio
	public Response getAuthorById (@PathParam("authorId") final Long authorId) throws BibliospringApiException {
        LOG.debug ("@GET - get author by ID");
        try {
			final Author author = authorService.getById(authorId);
			if (author !=null) {
				return Response.ok(authorAssembler.modelToDto(author)).build();
			} else {
				throw new BibliospringApiException ("Author not found");
			}
		} catch (final BibliospringServiceException e) {
            LOG.error("@GET by id error!", e);
			throw new BibliospringApiException (e.getMessage(), e);
		}
	}
	
	@GET
	public Response getAllAuthors() {
        LOG.debug ("@GET - get all authors");
        final List<Author> authorList = authorService.getAll();
        final List<AuthorDto> authorDtoList = new ArrayList<>();
        for (Author author : authorList) {
            authorDtoList.add(authorAssembler.modelToDto(author));
        }
        final GenericEntity<List<AuthorDto>> genericAuthorDtos = new GenericEntity<List<AuthorDto>> (authorDtoList) {};
        return Response.ok(genericAuthorDtos).build();
    }
	
	@POST
	@Consumes (MediaType.APPLICATION_JSON)
	public Response insertAuthor(final AuthorDto dto) {
        LOG.debug ("@POST - insert an author");
        try {
            Author author = authorAssembler.dtoToModel(dto);
            author = authorService.insertAuthor(author);
            return Response.ok(authorAssembler.modelToDto(author)).build();
        } catch (BibliospringServiceException e) {
            LOG.error("@GET by id error!", e);
            throw new BibliospringApiException("@GET by id error!", e);
        }
	}
	/*
	@Path ("/{authorId}")
	@PUT
	@Consumes (MediaType.APPLICATION_JSON)
	public Response updateAuthor (final AuthorDto adto) {
		Author author = authorAssembler.dtoToModel(adto);
		try {
			
		} catch (final BibliospringServiceException e) {
			throw new BibliospringApiException (e.getMessage(), e);
		}
	}
	
	@Path("/{authorId}")
	@DELETE
	@Consumes (MediaType.APPLICATION_JSON)
	public Response deleteAuthor (@PathParam ("authorId") final Long authorId) {
		
	}*/
	
	
}
